import Vue from "vue";
import VueRouter from "vue-router";
import module from "./module";
import store from "../store";
import Home from "@/views/home/index.vue";
import Category from '@/views/home/category.vue'
import Trolley from '@/views/home/trolley.vue'

Vue.use(VueRouter);

const routes = [
  ...module,
  {
    path: "/",
    name: "home",
    component: Home,
    meta: {
      keepAlive: true,
      showbar: true,
    }
  },
  {
    path: "/category",
    name: "category",
    component: Category,
    meta: {
      keepAlive: true,
      showbar: true,
    }
  },
  {
    path: "/trolley",
    name: "trolley",
    component: Trolley,
    meta: {
      keepAlive: true,
      showbar: true,
    }
  },
  {
    path: "/order",
    name: "order",
    component: () => {
         return import(/* webpackChunkName: "home" */ '@/views/order/index.vue')
    },
    meta: {
      keepAlive: true,
      showbar: false,
    }
  }
];


const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  routes
});

function toLogin () {

}

// 全局路由钩子函数 对全局有效
router.beforeEach((to, from, next) => {
  const { title, showbar, auth } = to.meta;
  let token = store.getters['login/token'];

  // 判断是否需要登录
  // if (auth === true && !store.state.app.token) {
  //   if (form.name === "Login") return;
  //   return toLogin(true, to.fullPath);
  // }

  // 修改标题
  document.title = title || "好多多商城";
  // 是否显示底部TAB BAR
  showbar === true ? store.commit("SHOW_TAB_BAR") : store.commit("HIDE_TAB_BAR");
  next();
  // if (auth) { // 需要登录
  //   if (token) {
  //     next()
  //   } else {
  //     next({
  //       path: '/login',
  //       query: {
  //         redirect: to.fullPath
  //       }
  //     })
  //   }
  // } else {
  //   next()
  // }
})

export default router;
